clear all
set more off, perm
capture log close
set maxvar 32767
global klmChinaReach2: env klmChinaReach2
global datapath "${klmChinaReach2}/Fuyao_Erlfang"
cd "$datapath\moments_dta"

use "$datapath\task_learning_fortran_0526.dta", clear
preserve
collapse indext levell levelc2 levelf2 levelg if indext != -99, by(ts_id)

merge 1:1 ts_id using alltask_rate, nogen

save alltask_rate_195, replace

restore

preserve
collapse indext levell levelc2 levelf2 levelg if indext != -99, by(ts_id)

merge 1:1 ts_id using enroll_1mo, nogen

save enroll_1mo_195, replace

restore

use "$datapath\data_task_panel_cluster_Feb24.dta", clear
capture drop temp
forval i=1/20{
	gen artc2_`i'=.
}

forval i=1/13{
	forval j=1/11{
		sum task if levelc2==`i' & repc2==`j'
		if (r(N)>0){
		   gen temp=task if levelc2==`i' & repc2==`j'
		   bys KIDID levelc2: egen temp2=max(temp)
		   replace artc2_`j'= temp2 if indc2==1 & levelc2==`i'
		   drop temp temp2
		}
	}
}

forval i=1/6{
	gen artf2_`i'=.
}

forval i=1/7{
	forval j=1/6{
		sum task if levelf2==`i' & repf2==`j'
		if (r(N)>0){
		   gen temp=task if levelf2==`i' & repf2==`j'
		   bys KIDID levelf2: egen temp2=max(temp)
		   replace artf2_`j'= temp2 if indf2==1 & levelf2==`i'
		   drop temp temp2
		}
	}
}

forval i=1/3{
	gen artg_`i'=.
}

forval i=1/9{
	forval j=1/3{
		sum task if levelg==`i' & repg==`j'
		if (r(N)>0){
		   gen temp=task if levelg==`i' & repg==`j'
		   bys KIDID levelg: egen temp2=max(temp)
		   replace artg_`j'= temp2 if indg==1 & levelg==`i'
		   drop temp temp2
		}
	}
}

forval i = 1/5{
	forval j = 1/12{
		gen ind_artc2_`i'_l`j' = 1 if artc2_`i' == 1 & levelc2 == `j'
		bys KIDID (levelc2): carryforward ind_artc2_`i'_l`j', replace
	}
}


forval i = 1/5{
	forval j = 1/7{
		gen ind_artf2_`i'_l`j' = 1 if artf2_`i' == 1 & levelf2 == `j'
		bys KIDID (levelf2): carryforward ind_artf2_`i'_l`j', replace
	}
}


forval i = 1/3{
	forval j = 4/9{
		gen ind_artg_`i'_l`j' = 1 if artg_`i' == 1 & levelg == `j'
		bys KIDID (levelg): carryforward ind_artg_`i'_l`j', replace
	}
}





**************************f2**************************

tempfile artl_rate
tempname table
postfile `table' str60(Dataset) l_condition l_focus mean se lb ub N using artf2_rate_pre1, replace

// post `table' ("Condtional on previous level") (0) (0) (0) (0) (0) (0) (0)
forval i = 1/6{
	local j = `i'+1
	if `i' == 1 | `i' == 3 | `i' == 4 {
	    local ubk = 5
	}
	else if `i' == 2 {
	    local ubk = 2
	}
	else if `i' == 5 {
	    local ubk = 3
	}
	else {
	    local ubk = 4
	}
	if `i'>=2 & `i'<=3 {
	    local ubn = 5
	}
	else if `i' == 1 {
	    local ubn = 2
	}
	else if `i' == 5 {
	    local ubn = 4
	}
	else if `i' == 4 | `i' == 6  {
	    local ubn = 3
	}
	forval k = 1/`ubk'{		
		forval n = 1/`ubn'{
			mean artf2_`n' if ind_artf2_`k'_l`i' == 1 & levelf2 == `j'
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' ("artf2_`n'") (`i') (`j') (`m') (`s') (`lb') (`ub') (`N')
		}
	}
}

postclose `table'



tempfile artl_rate
tempname table
postfile `table' str60(Dataset) l_condition l_focus mean se lb ub N using artf2_rate_pre2, replace

// post `table' ("Condtional on previous 2 level") (0) (0) (0) (0) (0) (0) (0)
forval i = 1/5{
	local j = `i'+ 2
	if `i' == 1 | `i' == 3 | `i' == 4 {
	    local ubk = 5
	}
	else if `i' == 2 {
	    local ubk = 2
	}
	else if `i' == 5 {
	    local ubk = 3
	}

	if `i'>=1 & `i'<=2 {
	    local ubn = 5
	}
	else if `i' == 4 {
	    local ubn = 4
	}
	else if `i' == 3 | `i' == 5  {
	    local ubn = 3
	}
	forval k = 1/`ubk'{		
		forval n = 1/`ubn'{
			mean artf2_`n' if ind_artf2_`k'_l`i' == 1 & levelf2 == `j'
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' ("artf2_`n'") (`i') (`j') (`m') (`s') (`lb') (`ub') (`N')
		}
	}
}

postclose `table'


tempfile artl_rate
tempname table
postfile `table' str60(Dataset) task_condition l_focus mean se lb ub N using artf2_rate_pretask, replace

forval i = 1/7{
	if `i' == 1 | (`i'>=3 & `i'<=4) {
	    local ubk = 4
	    local ubn = 5
	}
	else if `i' == 2 {
	    local ubk = 1
	    local ubn = 2
	}
	else if `i' == 6 {
	    local ubk = 3
	    local ubn = 4
	}
	else if `i' == 5 | `i' == 7 {
	    local ubk = 2
		local ubn = 3
	}
	forval k = 1/`ubk'{
		local j = `k' + 1
		forval n = `j'/`ubn'{
		    di `i' `ubk' `ubn'
			qui sum artf2_`n' if ind_artf2_`k'_l`i' == 1 & levelf2 == `i'
			mean artf2_`n' if ind_artf2_`k'_l`i' == 1 & levelf2 == `i'
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' ("artf2_`n'") (`k') (`i') (`m') (`s') (`lb') (`ub') (`N')
		}
	}
}

postclose `table'











**************************c2**************************


tempfile artl_rate
tempname table
postfile `table' str60(Dataset) l_condition l_focus mean se lb ub N using artc2_rate_pre1, replace

// post `table' ("Condtional on previous level") (0) (0) (0) (0) (0) (0) (0)
forval i = 1/11{
	local j = `i'+1
	if `i' <= 2 | (`i'>=5 & `i'<=8) | `i' == 11 {
	    local ubk = 5
	}
	else if (`i'>= 3 & `i'<= 4) | `i' == 9 {
	    local ubk = 2
	}
	else {
	    local ubk = 4
	}
	if `i' == 1 | (`i'>=4 & `i'<=7) | `i' == 10 {
	    local ubn = 5
	}
	else if (`i'>= 2 & `i'<= 4) | `i' == 8 {
	    local ubn = 2
	}
	else if `i' == 9 {
	    local ubn = 4
	}
	else if `i' == 11 {
	    local ubn = 3
	}
	forval k = 1/`ubk'{		
		forval n = 1/`ubn'{
			mean artc2_`n' if ind_artc2_`k'_l`i' == 1 & levelc2 == `j'
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' ("artc2_`n'") (`i') (`j') (`m') (`s') (`lb') (`ub') (`N')
		}
	}
}

postclose `table'



tempfile artl_rate
tempname table
postfile `table' str60(Dataset) l_condition l_focus mean se lb ub N using artc2_rate_pre2, replace

// post `table' ("Condtional on previous 2 level") (0) (0) (0) (0) (0) (0) (0)
forval i = 1/10{
	local j = `i'+ 2
	if `i' <= 2 | (`i'>=5 & `i'<=8) {
	    local ubk = 5
	}
	else if (`i'>= 3 & `i'<= 4) | `i' == 9 {
	    local ubk = 2
	}
	else {
	    local ubk = 4
	}
	if (`i'>=3 & `i'<=6) | `i' == 9 {
	    local ubn = 5
	}
	else if `i'<= 2  | `i' == 7 {
	    local ubn = 2
	}
	else if `i' == 8 {
	    local ubn = 4
	}
	else if `i' == 10 {
	    local ubn = 3
	}
	forval k = 1/`ubk'{		
		forval n = 1/`ubn'{
			mean artc2_`n' if ind_artc2_`k'_l`i' == 1 & levelc2 == `j'
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' ("artc2_`n'") (`i') (`j') (`m') (`s') (`lb') (`ub') (`N')
		}
	}
}

postclose `table'


tempfile artl_rate
tempname table
postfile `table' str60(Dataset) task_condition l_focus mean se lb ub N using artc2_rate_pretask, replace

forval i = 1/12{
	if `i' <= 2 | (`i'>=5 & `i'<=8) | `i' == 11 {
	    local ubk = 4
	    local ubn = 5
	}
	else if (`i'>= 3 & `i'<= 4) | `i' == 9 {
	    local ubk = 1
	    local ubn = 2
	}
	else if `i' == 10 {
	    local ubk = 3
	    local ubn = 4
	}
	else {
	    local ubk = 2
		local ubn = 3
	}
	forval k = 1/`ubk'{
		local j = `k' + 1
		forval n = `j'/`ubn'{
		    di `i' `ubk' `ubn'
			qui sum artc2_`n' if ind_artc2_`k'_l`i' == 1 & levelc2 == `i'
			mean artc2_`n' if ind_artc2_`k'_l`i' == 1 & levelc2 == `i'
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' ("artc2_`n'") (`k') (`i') (`m') (`s') (`lb') (`ub') (`N')
		}
	}
}

postclose `table'




**************************g**************************

tempfile artl_rate
tempname table
postfile `table' str60(Dataset) l_condition l_focus mean se lb ub N using artg_rate_pre1, replace

// post `table' ("Condtional on previous level") (0) (0) (0) (0) (0) (0) (0)
forval i = 4/8{
	local j = `i'+1
	if `i' == 4 | `i' == 7 | `i' == 8 {
	    local ubk = 3
	}
	else if `i' == 5 | `i' == 6  {
	    local ubk = 2
	}

	if `i'>=6 & `i'<=7 {
	    local ubn = 3
	}

	else if `i' == 4 | `i' == 5 | `i' == 8  {
	    local ubn = 2
	}
	forval k = 1/`ubk'{		
		forval n = 1/`ubn'{
			mean artg_`n' if ind_artg_`k'_l`i' == 1 & levelg == `j'
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' ("artg_`n'") (`i') (`j') (`m') (`s') (`lb') (`ub') (`N')
		}
	}
}

postclose `table'



tempfile artl_rate
tempname table
postfile `table' str60(Dataset) l_condition l_focus mean se lb ub N using artg_rate_pre2, replace

// post `table' ("Condtional on previous 2 level") (0) (0) (0) (0) (0) (0) (0)
forval i = 4/7{
	local j = `i'+ 2
	if `i' == 4 | `i' == 7 {
	    local ubk = 3
		local ubn = 2
	}
	else if `i' == 5 | `i' == 6 {
	    local ubk = 2
		local ubn = 3
	}

	forval k = 1/`ubk'{		
		forval n = 1/`ubn'{
			mean artg_`n' if ind_artg_`k'_l`i' == 1 & levelg == `j'
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' ("artg_`n'") (`i') (`j') (`m') (`s') (`lb') (`ub') (`N')
		}
	}
}

postclose `table'


tempfile artl_rate
tempname table
postfile `table' str60(Dataset) task_condition l_focus mean se lb ub N using artg_rate_pretask, replace

forval i = 4/9{
	if `i' == 5 | `i' == 6 | `i' == 9 {
	    local ubk = 1
	    local ubn = 2
	}
	else if `i' == 4 | `i' == 7 | `i' == 8 {
	    local ubk = 2
		local ubn = 3
	}
	forval k = 1/`ubk'{
		local j = `k' + 1
		forval n = `j'/`ubn'{
		    di `i' `ubk' `ubn'
			qui sum artg_`n' if ind_artg_`k'_l`i' == 1 & levelg == `i'
			mean artg_`n' if ind_artg_`k'_l`i' == 1 & levelg == `i'
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' ("artg_`n'") (`k') (`i') (`m') (`s') (`lb') (`ub') (`N')
		}
	}
}

postclose `table'


*******************first 5 tasks at each level*******************


tempfile artl_rate
tempname table
postfile `table' level artc2 mean se lb ub N using artc21-5_rate, replace


// post `table' ("Condtional previous task") (0) (0) (0) (0) (0) (0) (0)
// post `table' ("l_cdt now is the task controled") (0) (0) (0) (0) (0) (0) (0)

forval i = 1/12{
	forval n = 1/5{
		sum artc2_`n' if levelc2 == `i' & indc2 == 1
		if `r(N)' > 0{
			mean artc2_`n' if levelc2 == `i' & indc2 == 1
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' (`i') (`n') (`m') (`s') (`lb') (`ub') (`N')
		}
		else {
			post `table' (`i') (`n') (-99) (0) (0) (0) (`r(N)')
		}
	}
}



postclose `table'



tempfile artl_rate
tempname table
postfile `table' level artf2 mean se lb ub N using artf21-5_rate, replace


// post `table' ("Condtional previous task") (0) (0) (0) (0) (0) (0) (0)
// post `table' ("l_cdt now is the task controled") (0) (0) (0) (0) (0) (0) (0)

forval i = 1/6{
	forval n = 1/5{
		sum artf2_`n' if levelf2 == `i' & indf2 == 1
		if `r(N)' > 0{
			mean artf2_`n' if levelf2 == `i' & indf2 == 1
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' (`i') (`n') (`m') (`s') (`lb') (`ub') (`N')
		}
		else {
			post `table' (`i') (`n') (-99) (0) (0) (0) (`r(N)')
		}
	}
}



postclose `table'




tempfile artl_rate
tempname table
postfile `table' level artg mean se lb ub N using artg1-5_rate, replace


// post `table' ("Condtional previous task") (0) (0) (0) (0) (0) (0) (0)
// post `table' ("l_cdt now is the task controled") (0) (0) (0) (0) (0) (0) (0)

forval i = 4/9{
	forval n = 1/3{
		sum artg_`n' if levelg == `i' & indg == 1
		if `r(N)' > 0{
			mean artg_`n' if levelg == `i' & indg == 1
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' (`i') (`n') (`m') (`s') (`lb') (`ub') (`N')
		}
		else {
			post `table' (`i') (`n') (-99) (0) (0) (0) (`r(N)')
		}
	}
}



postclose `table'
